package com.wss.controller;

import com.alibaba.fastjson.JSON;
import com.wss.domain.Comment;
import com.wss.domain.Message;
import com.wss.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 添加视频评论
     */
    @RequestMapping(value="/addVideoComment",method = RequestMethod.POST)
    public String addVideoComment(Comment comment, Model model){
        // integer 可以赋值为空，将博客id赋值为空不会影响到数据库外键存储，但不存入参数会报错
        Integer integer = null;
        comment.setB_id(integer);
        commentService.addComment(comment);
        // 异步加载，添加完评论后根据v_id查询出视频的所有评论
        List<Comment> commentList = commentService.findCommentByVideoId(comment.getV_id());
        model.addAttribute("commentList",commentList);
        return "videoDetail::video_comment";
    }

    /**
     * 添加博客评论
     */
    @RequestMapping(value="/addBlogComment",method = RequestMethod.POST)
    public String addBlogComment(Comment comment, Model model){
        // integer 可以赋值为空,将视频id赋值为空不会影响到数据库外键存储，否则会报错
        Integer integer = null;
        comment.setV_id(integer);
        commentService.addComment(comment);
        //异步加载，添加完评论后根据b_id查询出视频的所有评论
        List<Comment> commentList = commentService.findCommentByBlogId(comment.getB_id());
        model.addAttribute("commentList",commentList);
        return "blogDetail::blog_comment";
    }

    /**
     * 删除评论
     */
    @RequestMapping(value="/deleteComment",method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String deleteComment(Comment comment){
        Message message = commentService.deleteComment(comment);
        String msg = JSON.toJSONString(message);
        return msg;
    }
}
